/**create by framework at 2019年05月30日 15:47:03**/
package com.star.truffle.module.fzjz.service;

import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.star.truffle.common.constants.EnabledEnum;
import com.star.truffle.core.StarServiceException;
import com.star.truffle.core.security.dto.StarUserInfo;
import com.star.truffle.core.web.ApiCode;
import com.star.truffle.core.web.service.BaseService;
import com.star.truffle.module.fzjz.cache.WarCategoryCache;
import com.star.truffle.module.fzjz.domain.WarCategory;
import com.star.truffle.module.fzjz.dto.req.WarCategoryRequestDto;
import com.star.truffle.module.fzjz.dto.res.WarCategoryResponseDto;

@Service
public class WarCategoryService extends BaseService {

  @Autowired
  private WarCategoryCache warCategoryCache;

  public Long saveWarCategory(WarCategory warCategory) {
    StarUserInfo user = getLoginInfo(null);
    warCategory.setEnabled(EnabledEnum.enabled.val());
    warCategory.setCreateTime(new Date());
    warCategory.setCreateUser(user.getUsername());
    warCategory.setUpdateTime(warCategory.getCreateTime());
    warCategory.setUpdateUser(warCategory.getCreateUser());
    if(null == warCategory.getType()) {
      warCategory.setType(1);
    }
    Integer idx = warCategoryCache.getMaxIdx();
    warCategory.setIdx(idx + 1);
    this.warCategoryCache.saveWarCategory(warCategory);
    return warCategory.getCateId();
  }

  public void updateWarCategory(WarCategoryRequestDto warCategoryRequestDto) {
    this.warCategoryCache.updateWarCategory(warCategoryRequestDto);
  }

  public void deleteWarCategory(Long cateId) {
    this.warCategoryCache.deleteWarCategory(cateId);
  }

  public void deleteWarCategory(String idstr) {
    if (StringUtils.isBlank(idstr)) {
      throw new StarServiceException(ApiCode.PARAM_ERROR);
    }
    String[] cateIds = idstr.split(",");
    for (String str : cateIds) {
      Long cateId = Long.parseLong(str);
      this.warCategoryCache.deleteWarCategory(cateId);
    }
  }

  public WarCategoryResponseDto getWarCategory(Long cateId) {
    WarCategoryResponseDto warCategoryResponseDto = this.warCategoryCache.getWarCategory(cateId);
    return warCategoryResponseDto;
  }

  public List<WarCategoryResponseDto> queryWarCategory(WarCategoryRequestDto warCategoryRequestDto) {
    List<WarCategoryResponseDto> list = this.warCategoryCache.queryWarCategory(warCategoryRequestDto);
//    list = list.stream().filter(item -> item.getChildNum() > 0).collect(Collectors.toList());
    return list;
  }

  public Long queryWarCategoryCount(WarCategoryRequestDto warCategoryRequestDto) {
    return this.warCategoryCache.queryWarCategoryCount(warCategoryRequestDto);
  }
  
  public void disabledCategory(String idstr) {
    if (StringUtils.isBlank(idstr)) {
      throw new StarServiceException(ApiCode.PARAM_ERROR);
    }
    String[] cateIds = idstr.split(",");
    for (String str : cateIds) {
      Long cateId = Long.parseLong(str);
      WarCategoryRequestDto dto = new WarCategoryRequestDto();
      dto.setCateId(cateId);
      dto.setEnabled(EnabledEnum.disabled.val());
      this.warCategoryCache.updateWarCategory(dto);
    }
  }

  public void enabledCategory(String idstr) {
    if (StringUtils.isBlank(idstr)) {
      throw new StarServiceException(ApiCode.PARAM_ERROR);
    }
    String[] cateIds = idstr.split(",");
    for (String str : cateIds) {
      Long cateId = Long.parseLong(str);
      WarCategoryRequestDto dto = new WarCategoryRequestDto();
      dto.setCateId(cateId);
      dto.setEnabled(EnabledEnum.enabled.val());
      this.warCategoryCache.updateWarCategory(dto);
    }
  }

  public void updateIdx(Long cateId, Long brotherId, String type) {
    WarCategoryResponseDto cate = this.warCategoryCache.getWarCategory(cateId);
    WarCategoryResponseDto brother = this.warCategoryCache.getWarCategory(brotherId);
    if(null == cate || null == brother) {
      throw new StarServiceException(ApiCode.PARAM_ERROR);
    }
    WarCategoryRequestDto c = new WarCategoryRequestDto();
    c.setCateId(cate.getCateId());
    c.setIdx(brother.getIdx());
    WarCategoryRequestDto b = new WarCategoryRequestDto();
    b.setCateId(brother.getCateId());
    b.setIdx(cate.getIdx());
    this.warCategoryCache.updateWarCategory(c);
    this.warCategoryCache.updateWarCategory(b);
  }

  public void updateIdx2(Long cateId, Long targetId, String type) {
    WarCategoryResponseDto cate = this.warCategoryCache.getWarCategory(cateId);
    WarCategoryResponseDto target = this.warCategoryCache.getWarCategory(targetId);
    if(null == cate) {
      throw new StarServiceException(ApiCode.PARAM_ERROR, "图层不存在，请刷新页面后重新操作");
    }
    WarCategoryRequestDto c = new WarCategoryRequestDto();
    c.setCateId(cate.getCateId());
    if("inner".equals(type)) {
      c.setParentId(target.getCateId());
      this.warCategoryCache.updateWarCategory(c);
    }else {
      if(cate.getParentId() == target.getParentId().longValue()) {
        c.setIdx(target.getIdx());
        WarCategoryRequestDto b = new WarCategoryRequestDto();
        b.setCateId(target.getCateId());
        b.setIdx(cate.getIdx());
        this.warCategoryCache.updateWarCategory(c);
        this.warCategoryCache.updateWarCategory(b);
      }else {
        c.setParentId(target.getParentId());
        if(cate.getIdx() < target.getIdx()) {
          if("prev".equals(type)) {
            this.warCategoryCache.updateWarCategory(c);
          }else {
            c.setIdx(target.getIdx());
            WarCategoryRequestDto b = new WarCategoryRequestDto();
            b.setCateId(target.getCateId());
            b.setIdx(cate.getIdx());
            this.warCategoryCache.updateWarCategory(c);
            this.warCategoryCache.updateWarCategory(b);
          }
        }else {
          WarCategoryRequestDto warCategoryRequestDto = new WarCategoryRequestDto();
          warCategoryRequestDto.setParentId(target.getParentId());
          warCategoryRequestDto.setLtIdx(cate.getIdx());
          List<WarCategoryResponseDto> list = this.warCategoryCache.queryWarCategory(warCategoryRequestDto);
          if(list.size() == 1) {
            if("next".equals(type)) {
              this.warCategoryCache.updateWarCategory(c);
            }else {
              c.setIdx(target.getIdx());
              WarCategoryRequestDto b = new WarCategoryRequestDto();
              b.setCateId(target.getCateId());
              b.setIdx(cate.getIdx());
              this.warCategoryCache.updateWarCategory(c);
              this.warCategoryCache.updateWarCategory(b);
            }
          }else {
            if("next".equals(type)) {
              list = list.stream().filter(dto -> dto.getCateId() > target.getCateId().longValue()).collect(Collectors.toList());
            }else {
              list = list.stream().filter(dto -> dto.getCateId() >= target.getCateId().longValue()).collect(Collectors.toList());
            }
            WarCategoryResponseDto first = list.get(0);
            c.setIdx(first.getIdx());
            this.warCategoryCache.updateWarCategory(c);
            for(int i = 0; i < list.size() - 1; i ++) {
              WarCategoryResponseDto temp = list.get(i);
              WarCategoryResponseDto temp2 = list.get(i + 1);
              WarCategoryRequestDto t = new WarCategoryRequestDto();
              t.setCateId(temp.getCateId());
              t.setIdx(temp2.getIdx());
              this.warCategoryCache.updateWarCategory(t);
            }
            WarCategoryResponseDto last = list.get(list.size() - 1);
            WarCategoryRequestDto t = new WarCategoryRequestDto();
            t.setCateId(last.getCateId());
            t.setIdx(cate.getIdx());
            this.warCategoryCache.updateWarCategory(t);
          }
        }
      }
    }
  }

}